package com.jinyou.chengfengshop.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jinyou.chengfengshop.Constant.AppUrl;
import com.jinyou.chengfengshop.OkHttpUtils.CallBackUtil;
import com.jinyou.chengfengshop.OkHttpUtils.OkhttpUtil;
import com.jinyou.chengfengshop.R;
import com.jinyou.chengfengshop.bean.RechareBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * 充值
 * Created by zhuzhiqiang on 2018/9/26.
 */

public class RechargeActivity extends AppCompatActivity implements View.OnClickListener{
    private RadioButton money_100;
    private RadioButton money_200;
    private RadioButton money_500;
    private RadioButton money_1000;
    private RadioButton money_2000;
    private RadioButton money_5000;

    private TextView sub_tv;
    private TextView back_tv;

    private int rechargeMoney = 0;

    private List<RechareBean> recBean;

    private String tocken;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        money_100 = (RadioButton) findViewById(R.id.money_100);
        money_100.setOnClickListener(this);
        money_200 = (RadioButton) findViewById(R.id.money_200);
        money_200.setOnClickListener(this);
        money_500 = (RadioButton) findViewById(R.id.money_500);
        money_500.setOnClickListener(this);
        money_1000 = (RadioButton) findViewById(R.id.money_1000);
        money_1000.setOnClickListener(this);
        money_2000 = (RadioButton) findViewById(R.id.money_2000);
        money_2000.setOnClickListener(this);
        money_5000 = (RadioButton) findViewById(R.id.money_5000);
        money_5000.setOnClickListener(this);

        sub_tv = (TextView) findViewById(R.id.sub_tv);
        sub_tv.setOnClickListener(this);
        back_tv = (TextView) findViewById(R.id.back_tv);
        back_tv.setOnClickListener(this);

        SharedPreferences sh = getSharedPreferences("user", Context.MODE_PRIVATE);
        tocken = sh.getString("tocken","");

        getRechargeList();


    }

    @Override
    public void onClick(View v) {

        if(recBean.size()<6){
            Toast.makeText(getApplicationContext(),"请稍后",Toast.LENGTH_SHORT).show();
            return;
        }
        switch (v.getId()){
            case R.id.money_100:
                setSlectFalse();
                money_100.setChecked(true);
                rechargeMoney = 100;
                break;
            case R.id.money_200:
                setSlectFalse();
                rechargeMoney = 200;
                money_200.setChecked(true);
                break;
            case R.id.money_500:
                setSlectFalse();
                rechargeMoney = 500;
                money_500.setChecked(true);
                break;
            case R.id.money_1000:
                setSlectFalse();
                rechargeMoney = 1000;
                money_1000.setChecked(true);
                break;
            case R.id.money_2000:
                setSlectFalse();
                rechargeMoney = 2000;
                money_2000.setChecked(true);
                break;
            case R.id.money_5000:
                setSlectFalse();
                rechargeMoney = 5000;
                money_5000.setChecked(true);

                break;
            case R.id.sub_tv:
                if(rechargeMoney == 0){
                    Toast.makeText(getApplicationContext(),"请选择充值金额",Toast.LENGTH_SHORT).show();
                }else {
                    if(rechargeMoney == 100){
                        CreatRecharge(recBean.get(0).getId(),recBean.get(0).getRechargeMoney());
                    }
                    if(rechargeMoney == 200){
                        CreatRecharge(recBean.get(1).getId(),recBean.get(1).getRechargeMoney());
                    }
                    if(rechargeMoney == 500){
                        CreatRecharge(recBean.get(2).getId(),recBean.get(2).getRechargeMoney());
                    }
                    if(rechargeMoney == 1000){
                        CreatRecharge(recBean.get(3).getId(),recBean.get(3).getRechargeMoney());
                    }
                    if(rechargeMoney == 2000){
                        CreatRecharge(recBean.get(4).getId(),recBean.get(4).getRechargeMoney());
                    }
                    if(rechargeMoney == 5000){
                        CreatRecharge(recBean.get(5).getId(),recBean.get(5).getRechargeMoney());
                    }


                }
                break;
            case R.id.back_tv:
                finish();
                break;
        }
    }


    private void setSlectFalse(){
        money_100.setChecked(false);
        money_200.setChecked(false);
        money_500.setChecked(false);
        money_1000.setChecked(false);
        money_2000.setChecked(false);
        money_5000.setChecked(false);
    }


    //获取充值列表
    private void getRechargeList(){

            HashMap<String,String> paramsMap = new HashMap<>();
            paramsMap.put("token",tocken);
            paramsMap.put("sysCustomer","o2o-chengfeng");

            OkhttpUtil.okHttpPost(AppUrl.RECHARGE_LIST, paramsMap, new CallBackUtil.CallBackString() {
                @Override
                public void onFailure(Call call, Exception e) {
                    Toast.makeText(RechargeActivity.this,"失败",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onResponse(String response) {//请求成功时调用，执行在UI线程
                    try {

                        JSONObject obj = new JSONObject(response);
                        int code = obj.getInt("status");
                        if(code == 1){
                            String ob = obj.getString("data");
                            if (ob != null && !ob.equals("")) {
                                Type typeOfT = new TypeToken<List<RechareBean>>() {
                                }.getType();
                                recBean = new Gson().fromJson(ob, typeOfT);
                            }

                            setRecText();

                        }else {
                            Toast.makeText(RechargeActivity.this,obj.getString("error"),Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });

    }


    //设置充值金额
    private void setRecText(){
        money_100.setText(recBean.get(0).getConsumeMoneyRmb()+"元");
        money_200.setText(recBean.get(1).getConsumeMoneyRmb()+"元");
        money_500.setText(recBean.get(2).getConsumeMoneyRmb()+"元");
        money_1000.setText(recBean.get(3).getConsumeMoneyRmb()+"元");
        money_2000.setText(recBean.get(4).getConsumeMoneyRmb()+"元");
        money_5000.setText(recBean.get(5).getConsumeMoneyRmb()+"元");
    }



    //创建充值订单
    private void CreatRecharge(String id,String money){

        HashMap<String,String> paramsMap = new HashMap<>();
        paramsMap.put("token",tocken);
        paramsMap.put("sysCustomer","o2o-chengfeng");
        paramsMap.put("rechargeId",id);
        paramsMap.put("rechargeMoney",money);


        OkhttpUtil.okHttpPost(AppUrl.RECHARGE_CREAT, paramsMap, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                Toast.makeText(RechargeActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {//请求成功时调用，执行在UI线程
                try {

                    JSONObject obj = new JSONObject(response);
                    int code = obj.getInt("status");
                    if(code == 1){
                        //JSONObject info = obj.getJSONObject("info");

                        String ob = obj.getString("info");
                        Log.d("CreatRecharge",ob);
                        Intent mIntent = new Intent(getApplicationContext(),SubmitRechargeActivity.class);
                        mIntent.putExtra("recinfo",ob);
                        startActivity(mIntent);
                    }else {
                        Toast.makeText(RechargeActivity.this,obj.getString("error"),Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }







}
